package com.itany.mall.dao.impl;

import java.util.List;

import com.itany.mall.dao.UserDao;
import com.itany.mall.dao.mapper.UserRowMapper;
import com.itany.mall.entity.User;
import com.itany.mall.factory.Component;
import com.itany.mall.jdbc.template.JDBCTemplate;

// 该类需要由工厂管理, 则加上工厂配套的注解
@Component
public class UserDaoImpl implements UserDao {

	@Override
	public User selectByLoginNameAndPassword(String name, String password) {
		UserRowMapper mapper = new UserRowMapper();
		StringBuffer sql = new StringBuffer(mapper.selectSql());
		sql.append(" where ");
		sql.append("     (u.name = ? or u.phone = ? or u.email = ?) ");
		sql.append("     and u.password = ? ");
		Object[] params = { name, name, name, password };
		List<User> list = new JDBCTemplate<Long, User>().query(sql.toString(), params, mapper);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

}

//File dir = new File(Database02.USERS_DIR);
//File[] fs = dir.listFiles(new FileFilter() {
//	@Override
//	public boolean accept(File pathname) {
//		return pathname.getName().endsWith(Database02.USER_SUFFIX);
//	}
//});
//try {
//	for (File f : fs) {
//		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
//		User u = (User) ois.readObject();
//		ois.close();
//		if ((u.getName().equals(name) || u.getPhone().equals(name)
//				|| u.getEmail().equals(name)) && u.getPassword().equals(password)) {
//			return u;
//		}
//	}
//} catch (Exception e) {
//	e.printStackTrace();
//}
//return null;
